package com.yhcui.monitor.dao;

import com.yhcui.monitor.bean.AppInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.List;

/**
 * 暂时不启用数据库，量不大，没有必要
 */
@Component
@Slf4j
public class AppInfoDao {

    @Value("${iot.link.apps}")
    private String apps;

    public List<AppInfo> queryAllAppInfo() {
        List<AppInfo> appInfoList = new ArrayList<>();

        if (apps == null || apps.trim().length() == 0) {
            return appInfoList;
        }
        String[] appsInfo = apps.split(";");
        for (String appInfo : appsInfo) {
            String[] apps = appInfo.split(",");
            if (apps.length < 5) {
                continue;
            }
            AppInfo appInfoObj = AppInfo.builder()
                    .appName(apps[0])
                    .domain(apps[1])
                    .port(Integer.valueOf(apps[2]))
                    .leader(apps[3])
                    .mobile(apps[4])
                    .build();
            appInfoList.add(appInfoObj);
        }

        return appInfoList;
    }
}
